//
//  AlertDialogView.m
//  PigMarket
//
//  Created by Felix on 2019/3/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AlertDialogView.h"

@interface AlertDialogView ()

/**
 builder
 */
@property (nonatomic , strong) AlertDialogBuilder *builder;

/**
 bgView
 */
@property (nonatomic , strong) UIView *bgView;

@end

@implementation AlertDialogView

#pragma mark - init method
- (instancetype)initWithAlertDialogBuilder:(AlertDialogBuilder *)builder {
    self = [super init];
    if (self) {
        self.builder = builder;
        
        self.backgroundColor = [UIColor clearColor];
        
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.cancelBtn];
    [self addSubview:self.bgView];
    if (self.builder.title) {
        [self.bgView addSubview:self.titleView];
        [self.titleView addSubview:self.titleLabel];
    }
    if (self.builder.contentText) {
        [self.bgView addSubview:self.contentLabel];
    }
    if (self.builder.contentView) {
        self.contentView = self.builder.contentView;
        [self.bgView addSubview:self.contentView];
    }
    // 积极的按钮
    if (self.builder.positiveBtnTitle) {
        [self.bgView addSubview:self.positiveBtn];
    }
}

- (void)initLayout {
    // 布局子控件
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-10);
    }];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.cancelBtn.mas_bottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
    if (self.builder.title) {
        [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.height.mas_equalTo(69);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        if (self.builder.contentText) {
            [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(24);
                make.right.mas_equalTo(-24);
                make.top.mas_equalTo(self.titleView.mas_bottom).offset(0);
            }];
            if (self.builder.contentView) {
                [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(24);
                    make.right.mas_equalTo(-24);
                    make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(0);
                }];
            }
        } else {
            if (self.builder.contentView) {
                [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(24);
                    make.right.mas_equalTo(-24);
                    make.top.mas_equalTo(self.titleView.mas_bottom).offset(0);
                }];
            }
        }
    } else {
        if (self.builder.contentText) {
            [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10);
                make.right.mas_equalTo(-10);
                make.top.mas_equalTo(20);
            }];
            if (self.builder.contentView) {
                [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(10);
                    make.right.mas_equalTo(-10);
                    make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10);
                }];
            }
        } else {
            if (self.builder.contentView) {
                [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(10);
                    make.right.mas_equalTo(-10);
                    make.top.mas_equalTo(20);
                }];
            }
        }
    }
    if (self.builder.positiveBtnTitle) {
        [self.positiveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(self.contentView ? self.contentView.mas_bottom : self.contentLabel.mas_bottom).offset(30);
            make.height.mas_equalTo(48);
        }];
    }
}

#pragma mark - getter and setter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 6;
    }
    return _bgView;
}
- (UIView *)titleView {
    if (!_titleView) {
        _titleView = [[UIView alloc] init];
    }
    return _titleView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor textMiddleGrayColor];
        _contentLabel.font = [UIFont middleFont];
        _contentLabel.textAlignment = self.builder.textAlignment;
        _contentLabel.numberOfLines = 0;
        
    }
    return _contentLabel;
}
//- (UIView *)contentView {
//    if (!_contentView) {
//        _contentView = [[UIView alloc] init];
//    }
//    return _contentView;
//}
- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.adjustsImageWhenHighlighted = NO;
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"alert_close"] forState:UIControlStateNormal];
    }
    return _cancelBtn;
}
- (UIButton *)positiveBtn {
    if (!_positiveBtn) {
        _positiveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_positiveBtn setBackgroundImage:[UIImage imageNamed:@"alert_bottombtn"] forState:UIControlStateNormal];
        [_positiveBtn setBackgroundImage:[UIImage imageNamed:@"alert_bottombtn_highlighted"] forState:UIControlStateHighlighted];
        [_positiveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _positiveBtn;
}

@end
